package temaihui.daimaitemai;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.alibaba.nb.android.trade.AliTradeEvent;
import com.alibaba.nb.android.trade.AliTradeSDK;
import com.alibaba.nb.android.trade.callback.AliTradeProcessCallback;
import com.alibaba.nb.android.trade.constants.AliTradeConstants;
import com.alibaba.nb.android.trade.model.AliOpenType;
import com.alibaba.nb.android.trade.model.AliTradeResult;
import com.alibaba.nb.android.trade.model.AliTradeShowParams;
import com.alibaba.nb.android.trade.model.AliTradeTaokeParams;
import com.alibaba.nb.android.trade.uibridge.AliTradeAddCartPage;
import com.alibaba.nb.android.trade.uibridge.AliTradeBasePage;
import com.alibaba.nb.android.trade.uibridge.AliTradeDetailPage;
import com.alibaba.nb.android.trade.uibridge.AliTradeMyCartsPage;
import com.alibaba.nb.android.trade.uibridge.AliTradeMyOrdersPage;
import com.alibaba.nb.android.trade.uibridge.AliTradePage;
import com.alibaba.nb.android.trade.uibridge.AliTradeService;
import com.alibaba.nb.android.trade.uibridge.AliTradeShopPage;
import com.alibaba.nb.android.trade.uibridge.IAliTradeService;
import com.alibaba.sdk.android.AlibabaSDK;
import com.alibaba.sdk.android.login.LoginService;
import com.alibaba.sdk.android.login.callback.LoginCallback;
import com.alibaba.sdk.android.login.callback.LogoutCallback;
import com.alibaba.sdk.android.session.model.Session;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sun on 16/8/14.
 */
public class AliUtil {
    private AliTradeShowParams aliTradeShowParams;//页面打开方式，默认，H5，Native
    private AliTradeTaokeParams aliTradeTaokeParams = null;//淘客参数，包括pid，unionid，subPid
    private Boolean isTaoke = false;//是否是淘客商品类型
    private String shopId = "60552065";//默认店铺id
    private Map<String, String> exParams;//yhhpass参数
    private IAliTradeService aliTradeService;
    private AliTradeBasePage aliTradeBasePage;//页面类型必填，不可为null
    private static AliUtil instance = new AliUtil();
    private AliUtil(){}

    public static AliUtil getInstance(){
        return instance;
    }
    public void showLogin(Activity activity) {
        LoginService loginService = AlibabaSDK.getService(LoginService.class);
        loginService.showLogin(activity, new LoginCallback() {

            @Override
            public void onSuccess(Session session) {
            }
            @Override
            public void onFailure(int code, String message) {

            }
        });
    }
    public void logout(Activity activity) {
        LoginService loginService = AlibabaSDK.getService(LoginService.class);
        loginService.logout(activity, new LogoutCallback() {
            @Override
            public void onFailure(int code, String msg) {

            }

            @Override
            public void onSuccess() {

            }
        });
    }

    public void showDetail(String itemId,Activity activity ) {

        aliTradeShowParams = new AliTradeShowParams(AliOpenType.Auto, false);
        aliTradeService = AliTradeSDK.getService(IAliTradeService.class);
        //itemId = "522166121586";//默认商品id

        exParams = new HashMap<>();
        exParams.put(AliTradeConstants.ISV_CODE, "appisvcode");
        exParams.put("alibaba", "阿里巴巴");//自定义参数部分，可任意增删改
        if (null == aliTradeService) {
            return;
        }

        aliTradeBasePage = new AliTradeDetailPage(itemId.trim());


        if (isTaoke) {
            aliTradeTaokeParams = new AliTradeTaokeParams("mm_26632322_6858406_23810104", "", "");
        } else {
            aliTradeTaokeParams = null;
        }

        aliTradeService.show(activity, aliTradeBasePage, aliTradeShowParams, aliTradeTaokeParams, exParams , new AliTradeProcessCallback() {
            @Override
            public void onTradeSuccess(AliTradeResult tradeResult) {

            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }




}
